package com.shenbianyun.bosskgpay.service.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.RequestMessage2;
import com.shenbianyun.bosskgpay.entity.ResponseMessage;
import com.shenbianyun.bosskgpay.entity.pay.PaymentReqModel;
import com.shenbianyun.bosskgpay.utils.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description //测试环境api接口调试 批量付款 接口6001
 * 不可固定业务参数
 **/


public class TestPaymentServiceD {



    public static Long levy_id = 35524L;//30481L、35503L(sby)、35524L(ycx)
    public static RequestMessage2 rm;
    public static PaymentReqModel paymentReqModel;
    public static PaymentReqModel.PayItems payItems;
    public static List payItemsList;


    public static void main(String[] args) throws Exception {

        TestPaymentServiceD.runTestPaymentService();
    }


    public static String runTestPaymentService() throws Exception {

        rm = new RequestMessage2();
        //生成guid码 理论上不重复
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test" + id);
        rm.setReqId("test" + System.currentTimeMillis() + 1);
        rm.setFunCode("6029");
        rm.setMerId(""+MerInfoConstant.MER_ID);
        rm.setVersion(MerInfoConstant.API_VERSION);


        /**--------————————————业务请求参数reqData——————————————————**/
        paymentReqModel = new PaymentReqModel();

        //商户批次号
        paymentReqModel.setMerBatchId("batch" + id );
        //服务商id
        paymentReqModel.setProviderId(levy_id);
        //任务id（项目id）
        paymentReqModel.setTaskId(MerInfoConstant.PROJECT_ID);
        //批量付款payItems字段
        payItemsList = new ArrayList();
        payItems = new PaymentReqModel.PayItems();
        //收款人名称
        //payItems.setPayeeName("刘华强");
        payItems.setPayeeName("崔方方");
        //身份证号
        //payItems.setIdCard("140581199007018359");
        payItems.setIdCard("412828199310044519");
        //收款人账号
        //payItems.setPayeeAcc("6228473764139314330");
        payItems.setPayeeAcc("6217000010071772738");//18001150364、6217000010071772738
        //金额（分）
        payItems.setAmt(111L);
        //收款人手机号
        //payItems.setMobile("15060019837");
        payItems.setMobile("18001150364");
        //备注 
        payItems.setMemo("api请求");
        //商户订单号，同批次内确保唯一不可重复。
        payItems.setMerOrderId("order" + id);
        //付款方式 0：银行卡，1：支付宝，2：微信
        payItems.setPaymentType(0);
        /**异步通知地址**/
        //payItems.setNotifyUrl("http://192.168.40.78:8081/pay/notify");

        payItemsList.add(payItems);
        paymentReqModel.setPayItems(payItemsList);


        String json = JsonUtils.toJson(paymentReqModel);
        System.out.println("请求未加密数据:" + json);


        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(MerInfoConstant.CHARSET_UTF_8), MerInfoConstant.INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MerInfoConstant.MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
        System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(MerInfoConstant.OMS_TEST_URL, "application/json", reqStr, "POST", MerInfoConstant.CHARSET_UTF_8);
        System.out.println("响应数据：" + resData);

        //断言
        JSONObject jsonObject1 = JSONObject.parseObject(resData);
        if (jsonObject1.get("resCode").equals("0000")) {
            System.out.println("批量付款结果:" + true);
            DES(resData);
        } else {
            System.out.println("批量付款结果:" + false + " " + jsonObject1.get("resMsg"));
        }



        return resData;
    }


    public static void DES(String resData) throws Exception {
        //返回信息：DES 解密
        ResponseMessage responseMessage = JSON.parseObject(resData, ResponseMessage.class);
        /** 2、Base64解编码 resDATA对象解密 **/
        String respData = responseMessage.getResData();
        byte[] base64bs = Base64Utils.decode(respData.getBytes());
        /** 3、DES 解密 **/
        String resultData = DESUtils.decryptToString(base64bs, MerInfoConstant.INTER_KEY);
        responseMessage.setResData(resultData);
        System.out.println("解密结果："+responseMessage);
    }



}
